package org.agridata.akvisualization.controller.immunity;

import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.agridata.akvisualization.doMain.entity.Immunity;
import org.agridata.akvisualization.result.Result;
import org.agridata.akvisualization.service.ImmunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/immunity")
@Api(tags = "春秋防疫、免疫信息")
public class ImmunityController {

    @Autowired
    private ImmunityService immunityService;

    @ApiOperation("免疫信息查询")
    @PostMapping("/statistics")
    public Result statistics() {
        return immunityService.statisticsImmunity();
    }

    @ApiOperation("春防秋防导入")
    @PostMapping("/defenseImport")
    public Result defenseImport(@RequestPart("file") MultipartFile file, String dates, String category, Integer cycle) {
        try {
            List<Immunity> memberList = EasyExcel.read(file.getInputStream())
                    .head(Immunity.class)
                    .sheet()
                    .doReadSync();
            return immunityService.defenseImportByCycle(memberList, dates, category, cycle);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("出现异常");
        }
    }

    @ApiOperation("春防秋防平台列表")
    @PostMapping("/defenseList")
    public Result defenseList(String category, Integer cycle, Long pageNo, Long pageSize) {
        return immunityService.selectDefenseList(category, cycle, pageNo, pageSize);
    }

    @ApiOperation("春防秋防编辑")
    @PostMapping("/updateDefense")
    public Result updateDefense(@RequestBody Immunity immunity) {
        return immunityService.updateDefenseById(immunity);
    }

    @ApiOperation("畜禽免疫下钻页")
    @PostMapping("progress")
    public Result progress() {
        return immunityService.progressThisYear();
    }


}